package util;

import java.net.SocketAddress;

import io.netty.channel.Channel;

public class RemotingUtil {
	public static String parseChannelRemoteAddr(final Channel channel) {
		if (null == channel) {
			return "";
		}
		SocketAddress remote = channel.remoteAddress();
		final String addr = remote != null ? remote.toString() : "";

		if (addr.length() > 0) {
			int index = addr.lastIndexOf("/");
			if (index >= 0) {
				return addr.substring(index + 1);
			}

			return addr;
		}

		return "";
	}

	public static String parseChannelLocalAddr(final Channel channel) {
		if (null == channel) {
			return "";
		}
		SocketAddress local = channel.localAddress();
		final String addr = local != null ? local.toString() : "";

		if (addr.length() > 0) {
			int index = addr.lastIndexOf("/");
			if (index >= 0) {
				return addr.substring(index + 1);
			}

			return addr;
		}

		return "";
	}

	public static String parseSocketAddressAddr(SocketAddress socketAddress) {
		if (socketAddress != null) {
			final String addr = socketAddress.toString();

			if (addr.length() > 0) {
				return addr.substring(1);
			}
		}
		return "";
	}
}
